package org.ucn.dmaa0914.temadesign.group2.tuiLayer;

import java.text.SimpleDateFormat;
import org.ucn.dmaa0914.temadesign.group2.StringUtil;
import org.ucn.dmaa0914.temadesign.group2.modelLayer.Person;
import org.ucn.dmaa0914.temadesign.group2.modelLayer.Loan;
import org.ucn.dmaa0914.temadesign.group2.modelLayer.Copy;
import org.ucn.dmaa0914.temadesign.group2.modelLayer.Dvd;

/**
 * Write a description of class Application here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Application
{
    public static Application instance = new Application();
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    
    private MainMenu mainMenu;
    private Menu currentMenu;
    
    private Person selectedPerson;
    private Loan selectedLoan;
    private Dvd selectedDvd;
    private Copy selectedCopy;
    
    private String resultText;
    
    /**
     * Constructor
     */
    public Application()
    {
        mainMenu = new MainMenu();
        currentMenu = mainMenu;
        resultText = "";
    }
    
    /**
     * Runs the application until no display is current no more.
     */
    public void run()
    {
        // If this is a seperate application object,
        // which is a sole solution for the BlueJ debugger,
        // launch run() on the singleton
        if( instance != this )
        {
            instance.run();
        }
        else
        {
            while( currentMenu != null )
                currentMenu.execute();
        }
    }
    
    public String getResultText()
    {
        return resultText;
    }
    
    public void setResultText(String text)
    {
        resultText = text;
    }
    
    public Menu getCurrentMenu()
    {
        return currentMenu;
    }
    
    public void setCurrentMenu(Menu menu)
    {
        this.currentMenu = menu;
    }
    
    public void setSelectedPerson(Person value)
    {
        this.selectedPerson = value;
    }
    
    public Person getSelectedPerson()
    {
        return selectedPerson;
    }
    
    public void setSelectedLoan(Loan value)
    {
        selectedLoan = value;
    }
    
    public Loan getSelectedLoan()
    {
        return selectedLoan;
    }
    
    public void setSelectedDvd(Dvd value)
    {
        selectedDvd = value;
    }
    
    public Dvd getSelectedDvd()
    {
        return selectedDvd;
    }
    
    public void setSelectedCopy(Copy value)
    {
        selectedCopy = value;
    }
    
    public Copy getSelectedCopy()
    {
        return selectedCopy;
    }
    
    public String makeStatusBar(int length)
    {
        String personStatus = selectedPerson == null ? "None" : selectedPerson.toString();
        String loanStatus = selectedLoan == null ? "None" : selectedLoan.toString();
        String dvdStatus = selectedDvd == null ? "None" : selectedDvd.toString();
        String copyStatus = selectedCopy == null ? "None" : selectedCopy.toString();
        
        String statusBar = StringUtil.centerAlign(
            StringUtil.centerAlign("Person: " + personStatus, length / 4 - 2) +
            StringUtil.centerAlign("Loan: " + selectedLoan, length / 4 - 2) +
            StringUtil.centerAlign("Dvd: " + selectedDvd, length / 4 - 2) +
            StringUtil.centerAlign("Copy: " + selectedCopy, length / 4 - 2),
            length);
            
        return statusBar;
    }
    
}
